package com.wenhai.concurrency.thread.schedule;

import java.util.concurrent.*;

/**
 * {@link ScheduledThreadPoolExecutor} 频率示例
 *
 * @author wenhai
 * @date   2021/4/29
 */
public class ScheduleThreadPoolExecutorRateDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ScheduledThreadPoolExecutor executor = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(2);
        System.out.println("当前时间：" + System.currentTimeMillis());
        ScheduledFuture<?> scheduledFuture = executor.scheduleAtFixedRate(() -> {
            System.out.println(" X = " + System.currentTimeMillis());
            try {
                TimeUnit.SECONDS.sleep(4);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(" Y = " + System.currentTimeMillis());
        }, 1, 2, TimeUnit.SECONDS);
//        executor.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
        executor.shutdown();
        System.out.println("已 shutdown");
    }
}
